package com.wyy.travels.controller;/*
 **
 * @author yangyang
 *@date 2022/07/23 23:49
 **/

import com.wyy.travels.entity.Place;
import com.wyy.travels.entity.Result;
import com.wyy.travels.service.PlaceService;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("place")
public class PlaceController {
    @Autowired
    private PlaceService placeService;
    @Value("${upload.dir}")
    private String realPath;

    @GetMapping("findAllPlace")
    public Map<String,Object> findAllPlace(Integer page,Integer rows,String provinceId){
        System.out.println(page+"===="+rows+"======"+provinceId);
        HashMap<String,Object> result=new HashMap<>();
        page = page == null ? 1 : page;
        rows = rows == null ? 3 : rows;
        List<Place> placeList = placeService.findByProvinceIdPage(page, rows, provinceId);
        Integer counts = placeService.findByProvinceIdCounts(provinceId);
        Integer totalPage= counts%rows==0?counts/rows:counts/rows+1;
        result.put("plcaes", placeList);
        result.put("page", page);
        result.put("counts", counts);
        result.put("totalPage", totalPage);
        return  result;
    }
    @GetMapping("delete")
    public Result delete(String id){
        System.out.println(":===delete====="+id);
        Result result=new Result();
        try {
            placeService.delete(id);
            result.setMsg("景点删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return  result.setState(false).setMsg(e.getMessage());
        }
        return  result;
    }

     @PostMapping("save")
     public  Result save(MultipartFile pic,Place place) throws IOException {
         Result result=new Result();
         try {
             String extension = FilenameUtils.getExtension(pic.getOriginalFilename());
             String  newfilename=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+extension;
             place.setPicpath(Base64Utils.encodeToString(pic.getBytes()));
             File file=new File(realPath);
             pic.transferTo(new File(file,newfilename));
             placeService.save(place);
             result.setMsg("景点保存成功");
         }catch (Exception e){
             e.printStackTrace();
             result.setState(false).setMsg(e.getMessage());
         }
       return  result;
     }

    @GetMapping("findOne")
    public  Place findOne(String id){
        Place place = placeService.findOne(id);
        return  place;
    }
     @PostMapping("update")
    public Result update(MultipartFile pic,Place place) throws IOException {
         Result result=new Result();
         try {
             String picpath = Base64Utils.encodeToString(pic.getBytes());
             place.setPicpath(picpath);
             String extension = FilenameUtils.getExtension(pic.getOriginalFilename());
             String  newfilename=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+extension;
             pic.transferTo(new File(realPath,newfilename));
             placeService.update(place);
             result.setMsg("景点修改成功");
         }catch (Exception e){
             e.printStackTrace();
             result.setState(false).setMsg(e.getMessage());
         }
         return  result;
    }



//    /**
//     * 根据省份id查询景点的方法
//     */
//    @GetMapping("findAllPlace")
//    public Map<String, Object> findAllPlace(Integer page, Integer rows, String provinceId) {
//        System.out.println(page+"===="+rows+"======"+provinceId);
//        HashMap<String, Object> result = new HashMap<>();
//        page = page == null ? 1 : page;
//        rows = rows == null ? 3 : rows;
//        //景点集合
//        List<Place> places = placeService.findByProvinceIdPage(page, rows, provinceId);
//        //处理分页
//        Integer counts = placeService.findByProvinceIdCounts(provinceId);
//        //总页数
//        Integer totalPage = counts % rows == 0 ? counts / rows : counts / rows + 1;
//        result.put("places", places);
//        result.put("page", page);
//        result.put("counts", counts);
//        result.put("totalPage", totalPage);
//        return result;
//    }

}
